			
				package com.cn.xml;
				
				import java.io.File;
				import java.io.FileOutputStream;
				import java.util.ArrayList;
				import java.util.List;
				import org.dom4j.Document;
				import org.dom4j.DocumentHelper;
				import org.dom4j.Element;
				import org.dom4j.io.OutputFormat;
				import org.dom4j.io.SAXReader;
				import org.dom4j.io.XMLWriter;
				import com.cn.bean.Student;
				import com.cn.utils.XMLUtils;
				
				public class Test {
			
					
						public static void main(String[] args) throws Exception {
							SAXReader  reader= new SAXReader();
							Document doc =reader.read(new File("src/student.xml"));
							Element root=doc.getRootElement();//根节点
							XMLUtils.getElement(root);
							System.out.println("------------------------------------------------");
							XMLUtils.setObject(root,Student.class);
								writeXML();
							List<Object> list =new ArrayList<Object>();
							list.add(new Student(1,"dq","1234",12,"江西"));
							list.add(new Student(2,"dq","1234",12,"江西"));
							list.add(new Student(3,"dq","1234",12,"江西"));
							list.add(new Student(4,"邓勤","1234",12,"江西"));
							XMLUtils.writeXML(list,Student.class);
						}
					
						/**
						 * 输出xml文件
						 * @throws Exception
						 */
						public static void writeXML() throws Exception{
							List<Student> list =new ArrayList<Student>();
							list.add(new Student(1,"dq","1234",12,"江西"));
							list.add(new Student(2,"dq","1234",12,"江西"));
							list.add(new Student(3,"dq","1234",12,"江西"));
							list.add(new Student(4,"邓勤","1234",12,"江西"));
							
							Element root=DocumentHelper.createElement("stu");//创建根节点
							Document doc=DocumentHelper.createDocument(root);
							
							for (Student student : list) {
								Element stu=DocumentHelper.createElement("student");
								Element id=DocumentHelper.createElement("id");
								id.setText(student.getId()+"");
								Element name=DocumentHelper.createElement("name");
								name.setText(student.getName());
								Element pass=DocumentHelper.createElement("pass");
								pass.setText(student.getPass());
								Element age=DocumentHelper.createElement("age");
								age.setText(student.getAge()+"");
								Element address=DocumentHelper.createElement("address");
								address.setText(student.getAddress());
								stu.add(id);
								stu.add(name);
								stu.add(pass);			
								stu.add(age);
								stu.add(address);
								root.add(stu);
							}
							FileOutputStream out =new FileOutputStream("stu.xml");
							OutputFormat format=new OutputFormat("   ",true);
							XMLWriter writer=new XMLWriter(out, format);
							writer.write(doc);
						}
					
				}
